<?php
    //This code cannot be executed on the same server as AD is installed on!!!
   
    //Connect
    //$ad = ldap_connect("ejc.ejercito.mil.co");
    $ad = ldap_connect("172.22.2.5");
    
   
    //Set some variables
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
   
    //Bind to the ldap directory
    $bd = ldap_bind($ad,"synchoid@ejc.ejercito.mil.co","directorio1")    
        or die("Couldn't bind to AD!");
    //$bd = ldap_bind($ad);
        
    //$solonecesito = array( "ou", "sn", "givenname", "mail", "CN", "samaccountname","uid");
        

    //Search the directory
    //$result = ldap_search($ad, "OU=orginizational unit,DC=domain,DC=com", "(CN=*)");
    //$result = ldap_search($ad, "DC=ejc,DC=ejercito,DC=mil,DC=co", "(CN=*)",$solonecesito,0,100000);
    $result = ldap_search($ad, "DC=ejc,DC=ejercito,DC=mil,DC=co", "(samaccountname=synchoid)");
    
    //Create result set
    $entries = ldap_get_entries($ad, $result);
   
    //Sort and print
    echo "User count: " . $entries["count"] . "<br /><br /><b>Users:</b><br />";

    for ($i=0; $i < $entries["count"]; $i++)
    {
        echo $entries[$i]["samaccountname"][0]."<br />";
    }

    //never forget to unbind!
    ldap_unbind($ad);
?>